package com.cskaoyan.gateway.controller.pay;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.pay.PaymentService;
import com.mall.pay.constant.PayRetCode;
import com.mall.pay.dto.PayRequest;
import com.mall.pay.dto.PayResponse;
import com.mall.pay.dto.PaymentStatusResponse;
import com.mall.pay.dto.PaymentStatusResquest;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author xudengfeng
 * @date 2020/12/22 20:34
 */

@RestController/*("/cashier")*/
@RequestMapping("/cashier")
public class AlipayController {

    @Reference(timeout = 3000, check = false)
    PaymentService paymentService;

    // TODO 支付完成修改订单状态
    @PostMapping("pay")
    public ResponseData pay(@RequestBody Map<String, String> map) {
        PayRequest payRequest = new PayRequest();
        payRequest.setMap(map);
        PayResponse response = paymentService.getAliPayQrCode(payRequest);
        String code = response.getCode();
        if (PayRetCode.SUCCESS.getCode().equals(code)) {
            return new ResponseUtil().setData(response.getPicUrl());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("queryStatus")
    public ResponseData queryStatus(String orderId) {

        PaymentStatusResquest resquest = new PaymentStatusResquest();
        resquest.setOrderId(orderId);
        PaymentStatusResponse paymentStatusResponse = paymentService.queryPaymentStatusByOrderId(resquest);

        String code = paymentStatusResponse.getCode();
        if (PayRetCode.SUCCESS_PAY.getCode().equals(code)) {
            return new ResponseUtil().setData(null);
        }

        return new ResponseUtil().setErrorMsg(paymentStatusResponse.getMsg());
    }
}
